﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace AvansRS.Domain
{
    public class EventDate
    {
        public virtual int ID { get; set; }
        public virtual bool ForStudents { get; set; }
        public virtual bool AllowChanges { get; set; }
        public virtual DateTime Date { get; set; }
        public virtual Event Event { get; set; }
        public virtual Rooster Rooster { get; set; }
        
        public virtual IList<Registration> Registrations { get; set; }

        public virtual void Register(Registration newRegistration)
        {
            foreach (Registration registration in Registrations)
            {
                if(registration.TimeBlock.ID == newRegistration.TimeBlock.ID)
                    throw new DomainException("Already an registration on this TimeBlock!");
            }

            newRegistration.EventDate = this;
            Registrations.Add(newRegistration);
        }

        public virtual ReadOnlyCollection<Registration> GetRegistrations()
        {
            ReadOnlyCollection<Registration> collection = new ReadOnlyCollection<Registration>(Registrations);
            return collection;
        }


        public virtual bool RegistrationTaken(TimeBlock block)
        {
            foreach (Registration registration in Registrations)
            {
                if(registration.TimeBlock == block)
                    return true;
            }

            return false;
        }

        public virtual bool TakenByEntity(Entity entity)
        {
            foreach (Registration registration in Registrations)
            {
                if(registration.Entity.ID == entity.ID)
                    return true;
            }
            return false;
        }

        public virtual bool TakenByEntityOnHour(Entity entity, TimeBlock block)
        {
            foreach (Registration registration in Registrations)
            {
                if (registration.Entity.ID == entity.ID && registration.TimeBlock.ID == block.ID)
                    return true;
            }
            return false;
            
        }

        public virtual Registration GetRegistrationOnHour(TimeBlock block)
        {
            foreach (Registration registration in Registrations)
            {
                if (registration.TimeBlock.ID == block.ID)
                    return registration;
            }
            return null;
        }
    }
}
